//修改手机号第二步
$(function() {

	// -------------------------------------------校验手机号是否可用--------------------------------------------------//
	// 校验手机号格式
	var phoneReg = /^1[3|4|5|7|8][0-9]{9}$/;// 手机号正则表达式
	var user_phoneFlag = $("#user_phoneFlag");
	function verifyPhone() {
		var phone = $("#u_telephone").val();// 获取用户输入的手机号
		if (!phoneReg.test(phone)) {
			$("#phoneUsedTip").css("display", "none");// 手机号被占用
			$("#ckphoneTip").css("display", "block");// 手机号格式错误
			user_phoneFlag.attr("value", "false");
			return false;
		} else {
			$("#phoneUsedTip").css("display", "none");
			$("#ckphoneTip").css("display", "none");
			return true;
		}
	}
	$("#u_telephone").bind("input propertychange change", function(event) {
		verifyPhone();
	});
	// 校验手机号是否被占用了
	$("#u_telephone").blur(function() {
		verifyPhoneUsed();
	});
	var sendPhoneCodeBtn = $("#sendPhoneCodeBtn");// 获取验证码按钮
	function verifyPhoneUsed() {
		var phone = $("#u_telephone").val();// 获取用户输入的手机号
		if (verifyPhone()) {
			$.ajax({
				type : 'post',
				url : '/tenement/verifyPhoneUsed.action',
				data : phone,
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					if (msg == "1") {// 为1表示手机号已经被占用了
						$("#phoneUsedTip").css("display", "block");
						user_phoneFlag.attr("value", "false");
					} else {
						$("#phoneUsedTip").css("display", "none");
						user_phoneFlag.attr("value", "true");// 该手机号可以使用
					}
				}
			})
		}
	}
	// 校验手机号方法2,提交时触发
	function verifyPhoneUsed2() {
		var phone = $("#u_telephone").val();// 获取用户输入的手机号
		if (verifyPhone()) {
			$.ajax({
				type : 'post',
				url : '/tenement/verifyPhoneUsed.action',
				data : phone,
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					if (msg == "1") {// 为1表示手机号已经被占用了
						$("#phoneUsedTip").css("display", "block");
						user_phoneFlag.attr("value", "false");
					} else {
						$("#phoneUsedTip").css("display", "none");
						user_phoneFlag.attr("value", "true");// 该手机号可以使用
					}
				}
			})
		}
	}
	// -------------------------------------------校验手机号是否可用--------------------------------------------------//

	// 发送手机验证码
	$("#sendPhoneCodeBtn").click(function() {
		verifyPhoneUsed2();
		var phoneFlag = user_phoneFlag.attr("value");
		if (phoneFlag == "true") {
			settime();
			var phoneNumber = $('#u_telephone').val();// 获取输入的新手机号
			// 如果手机号格式正确，未被绑定，则可以发生验证码
			$.ajax({
				type : 'post',
				url : '/tenement/sendPhoneCode.action',
				data : phoneNumber,
				contentType : "application/json;charset=utf-8",
				success : function(msg) {
					console.log("【手机验证码发送成功】" + msg);
				},
				error : function(msg) {
					console.log("【系统异常】");
				}
			})
		} else {
			return;
		}
	});
	// 获取验证码按钮可以点击
	function sendPhoneCodeBtnYes() {
		sendPhoneCodeBtn.removeClass("disabled");
		sendPhoneCodeBtn.attr("disabled", false);
	}
	// 获取验证码按钮不可点击
	function sendPhoneCodeBtnNo() {
		sendPhoneCodeBtn.attr("disabled", true);
		sendPhoneCodeBtn.addClass("disabled");
	}
	// 发送验证码倒计时60s
	var countdown = 10;
	function settime() {
		if (countdown == 0) {
			sendPhoneCodeBtn.html("获取验证码");
			if (user_phoneFlag.attr("value") == "true") {
				sendPhoneCodeBtnYes();
			} else {
				sendPhoneCodeBtnNo();
			}
			countdown = 10;
			return;
		} else {
			sendPhoneCodeBtn.html("重新发送(" + countdown + ")");
			sendPhoneCodeBtnNo();
			countdown--;
		}
		setTimeout(settime, 1000);
	}

	// 校验输入的手机验证码 codeTipNull codeTipError codeTipError2
	function verifyPhoneCode() {
		var code = $('#sms').val();// 获取输入的验证码的值
		console.log()
		if (code == "" || code.replace(/(^\s*)|(\s*$)/g, "") == "") {
			$("#codeTipNull").css("display", "block");// 验证码为空
			$("#codeTipError").css("display", "none");// 验证码错误
			$("#codeTipError2").css("display", "none");
			return false;
		} else {
			$("#codeTipError2").css("display", "none");// 提示用户重新获取验证码
			$("#codeTipError").css("display", "none");// 提示用户验证码错误
			$("#codeTipNull").css("display", "none");// 提示用户验证码为空
			return true;// 验证码不为空才能提交
		}
	}
	// 提交数据，下一步按钮
	$("#nextBtn").click(function() {
		verifyPhoneUsed2();// 校验手机号是否已经被绑定了
		var user_phoneFlag = $("#user_phoneFlag").attr("value");
		var codePhoneFlag = $("#codePhoneFlag").attr("value");
		console.log(user_phoneFlag + "  " + codePhoneFlag);
		if (user_phoneFlag != "false" && verifyPhoneCode()) {
			modifyPhone();
		}
	})
	// 修改用户手机号，数据提交
	function modifyPhone() {
		$("#codeTipNull").css("display", "none");
		$("#codeTipError").css("display", "none");
		$("#codeTipError2").css("display", "none");
		console.log("【修改手机号数据提交中...】");
		var u_id = $("#uId").val();
		var u_telephone = $("#u_telephone").val();
		var phoneCode = $('#sms').val();// 用户输入的手机验证码
		var data = {
			"u_id" : u_id,
			"u_telephone" : u_telephone,
			"phoneCode" : phoneCode
		};
		console.log(data);
		$
				.ajax({
					type : 'post',
					url : '/tenement/modifyPhoneNumber.action',
					data : JSON.stringify(data),
					contentType : "application/json;charset=utf-8",
					success : function(msg) {
						if (msg == "1") {
							$("#codeTipError2").css("display", "none");
							$("#codeTipError").css("display", "none");
							$("#codeTipNull").css("display", "none");
							window.location = "/tenement/toPersonModifyPhoneHtml3.action";
						} else if (msg == "-1") {
							$("#codeTipError").css("display", "none");
							$("#codeTipNull").css("display", "none");
							$("#codeTipError2").css("display", "block");// 提示用户重新获取验证码
						} else if (msg == "-2") {
							$("#codeTipError2").css("display", "none");
							$("#codeTipNull").css("display", "none");
							$("#codeTipError").css("display", "block");// 验证码错误
						}
					},
					error : function(msg) {
						console.log("【系统异常】");
					}
				});
	}
})